﻿using RuoVea.Entity;
using Mapster;
using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using RuoVea.Autowired;
using RuoVea.ExSqlSugar;

using RuoVea.ExIdGen;
using RuoVea.ExEnum;
using System;
using RuoVea.Util;

namespace RuoVea.Service
{
    /// <summary>
    /// 操作日志服务
    /// </summary>
    [AppService(ServiceLifetime.Transient)]
    public class SysOpLogService : ISysOpLogService
    {
        private readonly ISqlSugarRepository<SysLogOp> _sysOpLogRep; // 操作日志表仓储
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sysOpLogRep"></param>
        public SysOpLogService(ISqlSugarRepository<SysLogOp> sysOpLogRep)
        {
            _sysOpLogRep = sysOpLogRep;
        }

        /// <summary>
        /// 分页查询操作日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<dynamic> QueryOpLogPageList(OpLogInput input)
        {
            var opLogs = await _sysOpLogRep.Context.Queryable<SysLogOp>()
               .WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
               .WhereIF(Enum.IsDefined(typeof(YesOrNot), input.Success), u => u.Success == input.Success)
               .WhereIF(!string.IsNullOrWhiteSpace(input.SearchBeginTime), u => u.OpTime >= DateTime.Parse(input.SearchBeginTime.Trim()) && u.OpTime <= DateTime.Parse(input.SearchEndTime.Trim()))
               .OrderBy(u => u.Id, OrderByType.Desc)

               .Select<OpLogOutput>()
               .ToPagedListAsync(input.PageNo, input.PageSize);

            return opLogs.XnPagedResult();
        }

        /// <summary>
        /// 分页查询操作日志
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<dynamic> QueryUserOpLogPageList(OpLogInput input)
        { 
            var opLogs = await _sysOpLogRep.Context.Queryable<SysLogOp>()
               .InnerJoin<SysUser>((u, l) => u.Account == l.Account)

               .WhereIF(!string.IsNullOrWhiteSpace(input.Name), (u, l) => u.Name.Contains(input.Name.Trim()))
               .WhereIF(Enum.IsDefined(typeof(YesOrNot), input.Success), (u, l) => u.Success == input.Success)
               .WhereIF(!string.IsNullOrWhiteSpace(input.SearchBeginTime), (u, l) => u.OpTime >= DateTime.Parse(input.SearchBeginTime.Trim()) && u.OpTime <= DateTime.Parse(input.SearchEndTime.Trim()))
               .OrderBy((u, l) => u.Id, OrderByType.Desc)
               .Select((u, l) => new
               {
                   id = u.Id,
                   userName = l.Name,
                   page = u.Name,
                   MethodName = u.MethodName,
                   Success = u.Message,
                   OpTime = u.OpTime,
               })
               .ToPagedListAsync(input.PageNo, input.PageSize);

            return opLogs.XnPagedResult();
        }
        /// <summary>
        /// 清空操作日志
        /// </summary>
        /// <returns></returns>
        public async Task ClearOpLog()
        {
            await _sysOpLogRep.DeleteAsync(m => true);
        }
    }
}